/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/validate",
    "scprit/followup/app/healthAssess/healthQuest.servers.js",
    "scprit/followup/app/healthAssess/healthPatient.ui.js",
    "scprit/followup/app/question/question.ui.js",
    "scprit/followup/app/question/question.servers.js",
    "scprit/followup/app/question/question.event.js"],
    function ($,$bb,$validate,$healthQuestServers,$healthPatientUi,$questionUi,$questionServers,$questionEvent) {
    var healthQuest = {};

    /**
     * 保存成功后的回调函数
     */
    var getSaveCallback =  function(data,params){
        $bb.toastShow("保存成功",2000);
        var dataValue = {
            "busiId":$healthQuestServers.getPageParams().busiId,
            "updateAccept":data.updateAccept,
            "patientId":data.patientId,
            "extInfo":$healthQuestServers.getPageParams().extInfo
        };
        $bb.openPage(params.openPage,dataValue); 
    }
    /**
     * 点击保存按钮
     */
    var onSaveClick = function(){
        if( $validate.init() ){
            var params = $(this).data("params");
            var formId = $("#"+params.domId).find("form").attr("id");
            var questiones = $questionServers.getDataForDom(formId);
            $healthQuestServers.ajaxSaveQuest(questiones,getSaveCallback,params);  
        }
    }
    /**
     * 请求问卷成功后的回调函数
     */
    var ajaxQuestCallBack = function(data,params){
        $questionUi.init({
            id: params.domId,
            list: data.quesInfo,
            showIcon: true,
        });
        $questionEvent.init();
        $validate.blurInit();
        params = $.extend(params,{"quesName":data.quesName,"tmplVerInfo":data.tmplVerInfo});
        var topbars = [
            {
                "icon":"fa fa-floppy-o",
                "name":"保存",
                "callback":onSaveClick,
                "params":params
            }
        ];
        $bb.toolbar(topbars);
    }

    healthQuest.init = function(){
        $healthPatientUi.titInit({
            "id":"healthQuestTit",
            "tit":$healthQuestServers.getPageParams().extInfo.quesName
        });
        var params = {
            domId:"healthQuest",
            openPage:$healthQuestServers.getPageParams().goToRecord
        };
        $healthQuestServers.ajaxQuest(ajaxQuestCallBack,params);
    }

    return healthQuest;
});